# encoding=utf-8
import requests
from common.read_yaml import read_data
from config.read_config import Config
from settings.settings import Route
import os


class Request:
    """
    重写request方法
    """

    def __init__(self):
        self.domain = Config().read(Route().config('router.ini'), 'baseUrl', 'url')

    def request(self, filename):
        """

        :param filename: testcases目录下文件中的用例
        :return: 请求接口的响应数据（response）
        """
        # 读取testcases 目录下的文件的用例
        yamlData = read_data(Route().testcases(filename))

        url = self.domain + os.path.join(yamlData['request_url'])
        data = yamlData['request_param']
        headers = yamlData['request_header']
        if yamlData['request_method'] == "get":
            res = requests.get(url=url, params=data, headers=headers)
            result = res.json()
        else:
            if yamlData['data_format'] == "0":
                res = requests.post(url=url, data=data, headers=headers)
                result = res.json()
            else:
                res = requests.post(url.url, json=data, headers=headers)
                result = res.json()
            return result
        return result
